﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Leikjavefurinn.Context;

namespace Leikjavefurinn.Repo
{
    public class ScoreboardRepo
    {
        private LeikjavefurEntities db = new LeikjavefurEntities();

        //Næ í færslu í ScoreBoard fyrir ID
        public Scoreboard GetScoreBoard(Guid id)
        {
            return db.Scoreboard.SingleOrDefault(d => d.User == id);
        }

        //Næ í færslu í ScoreBoard fyrir notendanafn
        public Scoreboard GetScoreBoard(string userName)
        {
            var result = (from ux in db.Scoreboard
                          join u in db.Users on
                          ux.User equals u.UserId
                          where u.UserName == userName
                          select ux).SingleOrDefault();

            return result;
        }

        //Bý til færslu í ScoreBoard fyrir notendanafn
        public void CreateScoreBoard(string userName)
        {
            var user = db.Users.SingleOrDefault(n => n.UserName == userName);

            db.Scoreboard.Add(new Scoreboard { User = user.UserId });
            db.SaveChanges();
        }

        public void Save()
        {
            db.SaveChanges();
        }
    }
}
